<?php
namespace Modules\Admin\Controllers\Ucenter;

use S\Config;
use Modules\Admin\Model\Dao\Api\Ldap as ApiLdap;
use Modules\Admin\Model\Data\Validate as DataValidate;

class Sso extends Controller {

    /**
     * @name 引导登录页
     * @return bool
     */
    public function loginAction() {
        $return_url = APP_HOST_URL .'/admin/sso/index';
        $param = array(
            'service' => $return_url,
            'systemId' => Config::confServer('request.ucenter.system_id'),
        );
        $host = Config::confServer('request.ucenter.host');
        $url = $host .'login'.'?' . http_build_query($param);
        header("Location: $url");
        return true;
    }

    /**
     * @name 登录
     * @throws \Base\Exception\Service
     */
    public function indexAction() {
        $ticket = $this->getParams('ticket');
        $userinfo = (new ApiLdap())->validate($ticket, ApiLdap::IS_RETURN);
        (new DataValidate())->validate($ticket, $userinfo);
        $url = APP_HOST_URL .'/admin/welcome/index';
        header("Location: $url");
        $this->response['msg'] = '登录成功';
    }

    /**
     * @name 登出
     */
    public function logoutAction() {
        (new ApiLdap())->logout($_SESSION['ticket']);
        session_destroy();
        setcookie('menu', '', time()-1, APP_ADMIN_PATH);
        header('Location: '.APP_ADMIN_PATH.'/welcome/index');
    }
    
    /**
     * @name 跳转到登录页
     */
    public function goto_loginAction() {
        session_destroy();
        setcookie('menu', '', time()-1, APP_ADMIN_PATH);
        header('Location: '.APP_ADMIN_PATH.'/login/index');
    }

}
